Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(jsx): export ComponentWithChildren type #3758

Closed
wants to merge 1 commit into from

Conversation

yasuaki640
Copy link
Contributor

@yasuaki640 yasuaki640 commented Dec 19, 2024

When I run npm run type-check on this repository, I get the following type error.

npm run type-check 

> type-check
> tsc

src/middleware/layout.ts:4:45 - error TS2345: Argument of type 'FC' is not assignable to parameter of type 'ComponentWithChildren'.
  Type 'HtmlEscapedString | Promise<HtmlEscapedString> | null' is not assignable to type 'HtmlEscapedString | Promise<HtmlEscapedString>'.
    Type 'null' is not assignable to type 'HtmlEscapedString | Promise<HtmlEscapedString>'.

4 export const LayoutMiddleware = jsxRenderer(Layout);
                                              ~~~~~~


Found 1 error in src/middleware/layout.ts:4

I think the easiest way to fix this is to export the ComponentsWithChildren type.

type ComponentWithChildren = (

Since it can happen that a jsx renderer argument is defined in a different jsx file, I think exporting the type is a good idea, what do you think ?

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

@yasuaki640 yasuaki640 closed this Dec 19, 2024
@yasuaki640 yasuaki640 reopened this Dec 19, 2024
Copy link

codecov bot commented Dec 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.72%. Comparing base (18f937d) to head (cee75d6).
Report is 157 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3758      +/-   ##
==========================================
- Coverage   95.76%   91.72%   -4.04%     
==========================================
  Files         151      159       +8     
  Lines        9166    10178    +1012     
  Branches     2726     2891     +165     
==========================================
+ Hits         8778     9336     +558     
- Misses        388      841     +453     
- Partials        0        1       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yasuaki640 yasuaki640 changed the title export ComponentWithChildren type chore()export ComponentWithChildren type Dec 19, 2024
@yasuaki640 yasuaki640 changed the title chore()export ComponentWithChildren type chore(jsx): export ComponentWithChildren type Dec 19, 2024
@yusukebe
Copy link
Member

Hey @yasuaki640

In that case, you can use PropsWithChildren, and it will be fixed:

import type { PropsWithChildren } from 'hono/jsx'

export const Layout = ({ children }: PropsWithChildren) => (
// ...
)

In most cases, it is recommended to use PropsWithChildren for function components rather than FC because it will add a type to children(It might be better to rewrite the documentation).

If this solves the problem, I don't think we need this PR.

@yasuaki640
Copy link
Contributor Author

@yusukebe
Error fixed!
Thanks!

@yasuaki640 yasuaki640 closed this Dec 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants